/*     */ package com.yss.sofa.cml.extensions;
/*     */ import java.awt.Font;
/*     */ import java.awt.FontFormatException;
/*     */ import java.awt.GraphicsEnvironment;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.Collections;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import java.util.Set;
/*     */ import java.util.Vector;
/*     */ import java.util.concurrent.ConcurrentHashMap;

/*     */ 
/*     */ import com.yss.sofa.cml.log.Log;
/*     */ import com.yss.sofa.cml.log.LogFactory;
/*     */ 
/*     */ public class FontFamilyFactoryService
/*     */ {
/*  27 */   private static final Log log = LogFactory.getLog(FontFamilyFactoryService.class);
/*     */   private List<FontFamily> families;
/*     */   private Set<String> fontNames;
/*     */   private Set<String> registry;
/*     */   private Map<String, String> locales;
/*     */   private Map<String, File> fontFiles;
/*     */   private String modifier;
/*     */ 
/*     */   public static FontFamilyFactoryService getInstance()
/*     */   {
/*  34 */     return Holder.singleton;
/*     */   }
/*     */ 
/*     */   private FontFamilyFactoryService() {
/*  38 */     ensureInit();
/*  39 */     loadLocalFamilies();
/*  40 */     loadMapping();
/*     */   }
/*     */ 
/*     */   public List<FontFamily> getAvailableFontFamlies()
/*     */   {
/*  60 */     if (this.families == null) {
/*  61 */       return Collections.emptyList();
/*     */     }
/*  63 */     return Collections.unmodifiableList(this.families);
/*     */   }
/*     */ 
/*     */   public boolean registryFont(FontArtifact fa, boolean ignoreInstalled)
/*     */     throws Exception
/*     */   {
/*  75 */     ClassLoader ocl = Thread.currentThread().getContextClassLoader();
/*  76 */     ClassLoader ncl = getClass().getClassLoader();
/*  77 */     Thread.currentThread().setContextClassLoader(ncl);
/*  78 */     if (fa != null) {
/*  79 */       File fontFile = fa.getFontFile();
/*  80 */       if (fontFile == null) {
/*  81 */         throw new IllegalArgumentException("安装字体失败，字体文件为空。");
/*     */       }
/*     */       try
/*     */       {
/*  85 */         Font font = null;
/*     */         try {
/*  87 */           font = Font.createFont(0, fontFile);
/*     */         } catch (FontFormatException e) {
/*     */           try {
/*  90 */             font = Font.createFont(1, fontFile);
/*     */           } catch (FontFormatException ex) {
/*  92 */             throw ex;
/*     */           }
/*     */         }
/*  95 */         if (font != null) {
/*  96 */           String family = font.getFamily(Locale.ENGLISH);
/*  97 */           String fontName = font.getFontName(Locale.ENGLISH);
/*  98 */           String name = font.getName();
/*  99 */           if (!this.registry.contains(fontName)) {
/* 100 */             this.families.add(new FontFamily(family, name, fontName, fa.getFontFormat()));
/* 101 */             this.fontFiles.put(fontName, fontFile);
/* 102 */             this.registry.add(fontName);
/*     */           }
/* 104 */           if (this.fontNames.contains(fontName)) {
/* 105 */             String msg = "系统已安装字体：" + name + "，无需再次安装。";
/* 106 */             log.info(msg);
/* 107 */             if (!ignoreInstalled) {
/* 108 */               throw new Exception(msg);
/*     */             }
/* 110 */             boolean i = false;
/*     */             return i;
/*     */           }
/* 113 */           GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
/* 114 */           boolean success = ge.registerFont(font);
/* 115 */           if (success) {
/* 116 */             log.info("安装字体[" + name + "]成功。");
/* 117 */             this.fontNames.add(fontName);
/* 118 */             updateLastModifier();
/* 119 */             boolean j = true;
/*     */             return j;
/*     */           }
/* 121 */           log.info("安装字体" + name + "失败。");
/*     */         }
/*     */       }
/*     */       catch (IOException e) {
/* 125 */         throw e;
/*     */       } finally {
/* 127 */         Thread.currentThread().setContextClassLoader(ocl);
/*     */       }
/*     */     }
/* 130 */     return false;
/*     */   }
/*     */ 
/*     */   public void registryFontFile(File fontFile)
/*     */   {
/* 138 */     if (fontFile == null) {
/* 139 */       return;
/*     */     }
/*     */ 
/* 142 */     Font font = null;
/* 143 */     byte fontFormat = 0;
/*     */     try {
/* 145 */       fontFormat = 0;
/* 146 */       font = Font.createFont(0, fontFile);
/*     */     } catch (Exception e) {
/*     */       try {
/* 149 */         fontFormat = 1;
/* 150 */         font = Font.createFont(1, fontFile);
/*     */       } catch (Exception ex) {
/*     */       }
/*     */     }
/* 154 */     if (font != null) {
/* 155 */       String name = font.getName();
/* 156 */       String family = font.getFamily(Locale.ENGLISH);
/* 157 */       String fontName = font.getFontName(Locale.ENGLISH);
/* 158 */       if (!this.registry.contains(fontName)) {
/* 159 */         log.info("系统注册字体：" + name);
/* 160 */         this.families.add(new FontFamily(family, name, fontName, fontFormat));
/* 161 */         this.fontFiles.put(fontName, fontFile);
/* 162 */         this.registry.add(fontName);
/* 163 */         updateLastModifier();
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public Map<String, File> getFontFiles() {
/* 169 */     if (this.fontFiles == null) {
/* 170 */       return Collections.emptyMap();
/*     */     }
/* 172 */     return Collections.unmodifiableMap(this.fontFiles);
/*     */   }
/*     */ 
/*     */   private void updateLastModifier() {
/* 176 */     setLastModifier(Long.toString(System.currentTimeMillis()));
/*     */   }
/*     */ 
/*     */   public File getFontFile(String fontName) {
/* 180 */     return (File)this.fontFiles.get(fontName);
/*     */   }
/*     */ 
/*     */   public void addLocaleMapping(String locale, String en)
/*     */   {
/* 190 */     if ((locale != null) && (en != null))
/* 191 */       this.locales.put(locale.toLowerCase(), en);
/*     */   }
/*     */ 
/*     */   public String getEnglishMapping(String locale)
/*     */   {
/* 201 */     if (locale == null) {
/* 202 */       return null;
/*     */     }
/* 204 */     return (String)this.locales.get(locale.toLowerCase());
/*     */   }
/*     */ 
/*     */   public Map<String, String> getLocaleMapping()
/*     */   {
/* 211 */     if (this.locales == null) {
/* 212 */       return Collections.emptyMap();
/*     */     }
/* 214 */     return Collections.unmodifiableMap(this.locales);
/*     */   }
/*     */ 
/*     */   private void ensureInit() {
/* 218 */     if (this.fontNames == null) {
/* 219 */       synchronized (this) {
/* 220 */         if (this.fontNames == null) {
/* 221 */           this.fontNames = new HashSet();
/*     */         }
/*     */       }
/*     */     }
/* 225 */     if (this.registry == null) {
/* 226 */       synchronized (this) {
/* 227 */         if (this.registry == null) {
/* 228 */           this.registry = new HashSet();
/*     */         }
/*     */       }
/*     */     }
/* 232 */     if (this.families == null) {
/* 233 */       synchronized (this) {
/* 234 */         if (this.families == null) {
/* 235 */           this.families = new Vector();
/*     */         }
/*     */       }
/*     */     }
/* 239 */     if (this.locales == null) {
/* 240 */       synchronized (this) {
/* 241 */         if (this.locales == null) {
/* 242 */           this.locales = new ConcurrentHashMap();
/*     */         }
/*     */       }
/*     */     }
/* 246 */     if (this.fontFiles == null)
/* 247 */       synchronized (this) {
/* 248 */         if (this.fontFiles == null)
/* 249 */           this.fontFiles = new ConcurrentHashMap();
/*     */       }
/*     */   }
/*     */ 
/*     */   private void loadLocalFamilies()
/*     */   {
/* 256 */     Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
/* 257 */     if (fonts != null)
/* 258 */       for (Font font : fonts)
/* 259 */         this.fontNames.add(font.getFontName(Locale.ENGLISH));
/*     */   }
/*     */ 
/*     */   private void loadMapping()
/*     */   {
/* 265 */     ClassLoader ocl = Thread.currentThread().getContextClassLoader();
/* 266 */     ClassLoader ncl = getClass().getClassLoader();
/* 267 */     Thread.currentThread().setContextClassLoader(ncl);
/* 268 */     String fileName = Locale.CHINA + ".properties";
/* 269 */     InputStream stream = getClass().getResourceAsStream("/" + fileName);
/* 270 */     if (stream == null) {
/* 271 */       log.error("未加载到" + fileName + "文件。");
/* 272 */       return;
/*     */     }
/* 274 */     Properties props = new Properties();
/*     */     try {
/* 276 */       props.load(stream);
/* 277 */       Iterator itr = props.entrySet().iterator();
/* 278 */       while (itr.hasNext()) {
/* 279 */         Map.Entry entry = (Map.Entry)itr.next();
/* 280 */         String locale = (String)entry.getKey();
/* 281 */         String en = (String)entry.getValue();
/* 282 */         this.locales.put(locale, en);
/*     */       }
/*     */     } catch (IOException e) {
/* 285 */       log.error("加载到映射文件异常:" + e.getMessage(), e);
/*     */     } finally {
/* 287 */       if (stream != null) {
/*     */         try {
/* 289 */           stream.close();
/*     */         } catch (IOException e) {
/* 291 */           e.printStackTrace();
/*     */         }
/*     */       }
/* 294 */       Thread.currentThread().setContextClassLoader(ocl);
/*     */     }
/*     */   }
/*     */ 
/*     */   public String getLastModifier() {
/* 299 */     return this.modifier;
/*     */   }
/*     */ 
/*     */   public void setLastModifier(String modifier) {
/* 303 */     this.modifier = modifier;
/*     */   }
/*     */ 
/*     */   private static class Holder
/*     */   {
/*  30 */     private static FontFamilyFactoryService singleton = new FontFamilyFactoryService();
/*     */   }
/*     */ }

/* Location:           X:\repository\com\yss\sofa\cml\cml-api\1.1.0\cml-api-1.1.0.jar
 * Qualified Name:     com.yss.sofa.cml.extensions.FontFamilyFactoryService
 * JD-Core Version:    0.6.0
 */